import { Component, OnInit } from '@angular/core';
import {OutRecord} from '../../domain/admin';
import {ServiceLogicService} from '../../service/service-logic.service';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'app-out-application',
  templateUrl: './out-application.component.html',
  styleUrls: ['./out-application.component.css']
})
export class OutApplicationComponent implements OnInit {
  displayDialog: boolean;
  selectedOutRecord: OutRecord;
  newOutRecord: boolean;
  private data: any;
  cols: any[];
  approved_outRecords: OutRecord[];
  rejectAndPass_outRecords: OutRecord[];
  outRecord: OutRecord = {};
  onRowSelect(event) {
    this.newOutRecord = false;
    this.outRecord = this.cloneOutRecord(event.data);
    this.displayDialog = true;
  }
  showDialogToAdd() {
    this.newOutRecord = true;
    this.outRecord = {};
    this.displayDialog = true;
  }
  save() {
    const outRecords = [...this.approved_outRecords];
    if (this.newOutRecord) {
      outRecords.push(this.outRecord);
      console.log(this.outRecord);
      console.log(12);
      this.outRecord.state = 1;
      this.outRecord.des = '审核中';
      this.outRecord.admin_id = this.data.admin_id;
      this.outRecord.days = parseInt(this.outRecord.days, 0);
      this.logicService.addOutRecord(this.outRecord).then(res => {outRecords[outRecords.length - 1].id = res.id; });
      // 在这里像数据库写入数据。
    } else {
      outRecords[this.approved_outRecords.indexOf(this.selectedOutRecord)] = this.outRecord;
      this.outRecord.days = parseInt(this.outRecord.days, 0);
      this.outRecord.state = parseInt(this.outRecord.state, 0);
      this.logicService.updateOutRecord(this.outRecord).then(() => {});
      console.log(this.outRecord);
    }
    this.approved_outRecords = outRecords;
    this.outRecord = null;
    this.displayDialog = false;
  }
  delete() {
    // noinspection TsLint
    const index = this.approved_outRecords.indexOf(this.selectedOutRecord);
    this.logicService.deleteOutRecord(this.approved_outRecords[index].id).then(() => {
      this.approved_outRecords = this.approved_outRecords.filter((nmnm, i) => i !== index);
    });
    // 通过outRecords[this.outRecords.indexOf(this.selectedOutRecord)]删除数据库数据
    // console.log(index);
    // console.log(this.outRecords);
    this.outRecord = null;
    this.displayDialog = false;
  }
  cloneOutRecord(c: OutRecord): OutRecord {
    const outRecord = {};
    for (const prop in c) {
      if (c.hasOwnProperty(prop)) {
        outRecord[prop] = c[prop];
      }
    }
    return outRecord;
  }
  constructor(private logicService: ServiceLogicService, private activateRoute: ActivatedRoute) {
  }
  ngOnInit() {
    this.activateRoute.queryParams.subscribe(params => {
      this.data = params;
    });
    this.logicService.getOutRecords(1, this.data.admin_id).then(res => {this.approved_outRecords = res;
    console.log(res);
    console.log(this.approved_outRecords); });
    this.logicService.getOutRecords(5, this.data.admin_id).then(res => {this.rejectAndPass_outRecords = res; });
    this.cols = [
      {field: 'satrt_time', header: 'satrt_time'},
      {field: 'end_time', header: 'end_time'},
      {field: 'days', header: 'days'},
      {field: 'reason', header: 'reason'},
      {field: 'des', header: 'des'},
    ];
  }
}

